package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.VipDepositDetail;
import com.ruoyi.web.dto.VipDepositDetailAddDTO;
import com.ruoyi.web.dto.VipDepositDetailConditionDTO;
import com.ruoyi.web.dto.VipDepositDetailEditDTO;
import com.ruoyi.web.schedule.VipDepositDetailTask;
import com.ruoyi.web.service.IVipDepositDetailService;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * vip押金明细Controller
 *
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/vip_deposit_detail")
@AllArgsConstructor
public class VipDepositDetailController extends BaseController {

    private final IVipDepositDetailService vipDepositDetailService;
    private final VipDepositDetailTask vipDepositDetailTask;

    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @return {@link TableDataInfo}
     * @since 2023/3/27 15:39
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(VipDepositDetailConditionDTO condition) {
        startPage();
        List<VipDepositDetail> list = vipDepositDetailService.selectVipDepositDetailList(condition);
        return getDataTable(list);
    }

    /**
     * 导出vip押金明细列表
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:export')")
    @Log(title = "vip押金明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VipDepositDetailConditionDTO condition) {
        List<VipDepositDetail> list = vipDepositDetailService.selectVipDepositDetailList(condition);
        ExcelUtil<VipDepositDetail> util = new ExcelUtil<VipDepositDetail>(VipDepositDetail.class);
        util.exportExcel(response, list, "vip押金明细数据");
    }

    /**
     * 获取vip押金明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(vipDepositDetailService.selectVipDepositDetailById(id));
    }

    /**
     * 新增
     *
     * @param dto dto
     * @return {@link AjaxResult}
     * @since 2023/3/27 14:08
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:add')")
    @Log(title = "vip押金明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated VipDepositDetailAddDTO dto) {
        vipDepositDetailService.insertVipDepositDetail(dto);
        return success();
    }

    /**
     * 修改
     *
     * @param dto dto
     * @return {@link AjaxResult}
     * @since 2023/3/27 14:21
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:edit')")
    @Log(title = "vip押金明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated VipDepositDetailEditDTO dto) {
        vipDepositDetailService.updateVipDepositDetail(dto);
        return success();
    }

    /**
     * 删除vip押金明细
     */
    @PreAuthorize("@ss.hasPermi('vip_deposit_detail:remove')")
    @Log(title = "vip押金明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        vipDepositDetailService.deleteVipDepositDetailByIds(ids);
        return success();
    }

    /**
     * 立即检查到期记录
     *
     * @return {@link AjaxResult}
     * @since 2023/3/27 16:28
     */
    @Async
    @PostMapping("/check/expire")
    public AjaxResult checkExpire() {
        vipDepositDetailTask.task();
        return success();
    }
}
